<?php
class Category
{
	private $database;
	private $categoryID;
	private $categories = array();
	private $categoryLevel;
	private $categoryName;
	private $categoryDescription;
	private $numResources = 0; // just this category not including subcategories
	private $numTotalResources = 0; // include subcategories
	
	private $displayLetter;
	private $displayStart;
	private $displayLimit;
	
	private $page;
	
	public function __construct($database,$categoryID)
	{
		$this->database = $database->Recopy();
		$this->categoryID = $categoryID;
		$this->categoryLevel = strlen(strval($categoryID));
		$sql = "SELECT CategoryName, CategoryDescription FROM CategoryNames WHERE CategoryNameID=".$this->categoryID;
		$this->database->ExecuteSQL($sql);
		$row = $this->database->getRow();
		$this->categoryName=$row['CategoryName'];
		$this->categoryDescription=$row['CategoryDescription'];
		
		//get sub categories
		
		$sql = "SELECT CategoryNameID FROM CategoryNames WHERE CategoryNameID LIKE '".$this->categoryID."_'";
		$this->database->ExecuteSQL($sql);
		
		while($row = $this->database->getRow())
		{
			array_push($this->categories, new Category($this->database->Recopy(),$row['CategoryNameID']));
		}
		
		foreach ($this->categories as $category)
		{
			$this->numTotalResources += $category->getNumTotalResources();
		}
		
		$sql =  "SELECT res.*,cat.* FROM Resources as res ";
		$sql .= "INNER JOIN Categorys AS cat ON res.ResourceID=cat.ResourceID ";
		$sql .= "WHERE DeletedBy = '0' AND StatusTypeID = '3' AND cat.CategoryNameID=".$this->categoryID." ";
//		echo $sql."<br>\n";
		$this->database->ExecuteSQL($sql);
		
		$this->numResources += $this->database->getNumRows();
		$this->numTotalResources += $this->database->getNumRows();
	}
	
	private function addNBSP($string)
	{
		return str_replace(' ','&nbsp;',$string);
	}
	
	public function getNumTotalResources()
	{
		return $this->numTotalResources;
	}
	
	public function getNumResources()
	{
		return $this->numResources;
	}
	
	public function setPage($page)
	{
		$this->page = $page;
	}
	
	public function setDisplayLetter($displayLetter)
	{
		$this->displayLetter = $displayLetter;
	}
	
	public function setDisplayStart($displayStart)
	{
		$this->displayStart = $displayStart;
	}
	
	public function setDisplayLimit($displayLimit)
	{
		$this->displayLimit = $displayLimit;
	}
	
	public function getCategoryID()
	{
		return $this->categoryID;
	}
	
	public function getCategoryName()
	{
		return $this->categoryName;
	}
	
	public function getCategoryDescription()
	{
		return $this->categoryDescription;
	}
	
	public function getCategoryLevel()
	{
		return $this->categoryLevel;
	}
	
	public function getNavigationHTML()
	{
		$html = "<div class=\"CategoryNavigation\">\n";
		$strCategory = strval($this->categoryID);
		$fullName = "";
//		echo "catLevel = ".$this->categoryLevel."<br>\n";
		for ($i=0;$i<$this->categoryLevel;$i++)
		{
			$cat = new Category($this->database->Recopy(),$strCategory);
			$span = new Span();
			$span->setName("LinkShowCategoryElement".$cat->getCategoryID());
			$span->setCSSClass("CategoryNavigation");
			$span->setOnClick("ShowCategory('".$cat->getCategoryID()."')");
			$span->setOnmouseover("OnMouseOverShowCategory('LinkShowCategoryElement".$cat->getCategoryID()."')");
			$span->setOnmouseout("OnMouseOutShowCategory('LinkShowCategoryElement".$cat->getCategoryID()."')");
			$span->setText($this->addNBSP($cat->getCategoryName()." (".$cat->numResources."/".$cat->numTotalResources.")"));
			$fullName = $span->getHTML()."/&nbsp;".$fullName;
//			echo "FullName = ".$fullName."<br>\n";
			$strCategory = substr($strCategory,0,-1);
		}
		
		$html .= "<p><b>Current Category:</b> ".substr($fullName,0,-7)."</p>\n";
		
		$fullName = "";
//		echo "catLevel = ".$this->categoryLevel."<br>\n";
		if(count($this->categories)>0)
		{
			foreach ($this->categories as $category)
			{
				$span = new Span();
				$span->setName("LinkShowCategoryElement".$category->getCategoryID());
				$span->setCSSClass("CategoryNavigation");
				$span->setOnClick("ShowCategory('".$category->getCategoryID()."')");
				$span->setOnmouseover("OnMouseOverShowCategory('LinkShowCategoryElement".$category->getCategoryID()."')");
				$span->setOnmouseout("OnMouseOutShowCategory('LinkShowCategoryElement".$category->getCategoryID()."')");
				$span->setText($this->addNBSP($category->getCategoryName())."&nbsp;(".$category->getNumResources()."/".$category->getNumTotalResources().")");
				$fullName = $span->getHTML()."|&nbsp;".$fullName;
			}
			$html .= "<p><b>Choose a sub-category:</b> ".substr($fullName,0,-7)."</p>\n";
		}
		
		
		
		
		
		$html .= "</p>";
		
		$html .= "</div>\n";
		
		return $html;
		
	}
	
	public function getResources()
	{
		$resources = array();
		
		// if no alphabet letter or display limit specified, default is All letters, and 20 items
		
//		if(isset)
		
		$sql =  "SELECT res.*,cat.* FROM Resources as res ";
		$sql .= "INNER JOIN Categorys AS cat ON res.ResourceID=cat.ResourceID ";
		$sql .= "WHERE DeletedBy = '0' AND StatusTypeID = '3' AND cat.CategoryNameID=".$this->categoryID." ";
		if($this->displayLetter<>'')
			$sql .= "AND ResourceTitle LIKE '".$this->displayLetter."%' ";
		$sql .= "ORDER BY ResourceTitle ";
		$sql .= "LIMIT ".$this->displayStart.",".$this->displayLimit;
//		echo $sql."<br>\n";
		$this->database->ExecuteSQL($sql);
		while($row = $this->database->getRow())
		{
			$resource = new Resource($this->database->Recopy(),$row['ResourceID']);
			$resource->setCurrentUserGroup($this->page->getUserGroup());
			//			$resource->loadData($row['ResourceID'],$row['ResourceTitle'],$row['ResourceDescription'],$row['ResourceTypeID'],"","",,,,);
			array_push($resources,$resource);
		}
				
		return $resources;
		
	}
	
	
}

?>